$(document).ready(function(){
	cannotEnter();
	getVerifyCode();
	clickToGetNewCode();
	banInputSpace();
	loginValidate();
	login();
});
//登录
function login(){
	$("#submitBtn").click(function(){
		if(isLoginValidate.checkForm()){
			if($("#account").val()=="admin"&&$("#password").val()=="icui4cu"){
				location.href = "../../manager/index.html";
				return;
			}
			var day;
			if($("#loginForm input[type='checkbox']").is(':checked')){
				day = 7;
			}else{
				day = 0;
			}
			var url = "/HyperIT/user/login";
			var data = "account="+$("#account").val()+"&password="+$("#password").val()+"&verifyCode="+$("#verifyCode").val();
			$.post(url,data,function(data){
				if(data.serviceResult){
					var value = JSON.stringify(data.resultParm.userdetail);
					setCookie("user",value,day);
					location.href = "../index.html";
				}else{
					alert(data.resultInfo);
				}
			});
		}else{
			isLoginValidate.showErrors();
		}
	});
}
//登录表单验证
function loginValidate(){
	isLoginValidate = $("#loginForm").validate({
		onsubmit: false,
		onkeyup: false, 
		onfocusout: function(element){
			$(element).valid();
	  	},
   		errorPlacement: function(error, element) {  
   			error.appendTo(element.parent());
   		},
		rules:{
			account:{
				required:true
//				email:true
			},
			password:{
				required:true,
				minlength:6,
				maxlength:16
			},
			verifyCode:{
				required:true
			}
		},
		messages:{
			account:{
				required:"请输入账号！"
//				email:"请输入邮箱测正确格式！"
			},
			password:{
				required:"请输入密码！",
				minlength:"密码不能小于6个字符！",
				maxlength:"密码不能多于16个字符！"
			},
			verifyCode:{
				required:"请输入验证码！"
			}
		}
	});
}
//屏蔽空格输入
function banInputSpace(){
	$("input").focus(function(){
		$(document).keydown(function(e){
			var keycode;
			if(window.event){
				keycode = e.keyCode;
			}else if(e.which){
				keycode = e.which;
			}if(keycode==32){
				return false;
			}
			return true;
		});
	});
}
//获取图片验证码
function getVerifyCode(){
	var url = "/HyperIT/user/getImgVerifyCode";
	$.get(url, function(data){
		if(data.serviceResult){
			$("#verifyCodeImg img").attr("src","../../"+data.resultParm.path);
		}else{
			return false;
		}
	});
}
//点击验证码，获取新的验证码
function clickToGetNewCode(){
	$("#verifyCodeImg img").click(function(){
		console.log("点击获取新的图片验证码.");
		getVerifyCode();
	});
}